package globals;

public class FreeVector {
	float magnitude, angle;
	public FreeVector(){ // empty constructor
		magnitude=0;
		angle=0;
	}
	public FreeVector(float m, float a){ // Magnitude @ Theta constructor
		magnitude=m;
		angle=a;
	}
	public float getMagnatude(){return magnitude;}
	public float getAngle(){return angle;}
	public void setMagnitude(float m){
		if(m<0){
			m*=-1;
			setAngle(this.angle+(float)Math.PI);
		}
		this.magnitude=m;
	}
	public void setAngle(float a){
		// angle = coAngle of theta
		while(a>(2*Math.PI)){
			a-=(2*Math.PI);
		}
		while(a<0){
			a+=(2*Math.PI);
		}
		this.angle=a;
	}
	public float getScalar(){
		// |A(x,y)| = | sqrt( x^2 + y^2 ) | 
		return (float) Math.abs(Math.sqrt(Math.pow(this.getX(), 2) + Math.pow(this.getY(), 2)));
	}
	public float getX(){
		return(float)(this.magnitude*Math.cos(this.angle));
	}
	public float getY(){
		return(float)(this.magnitude*Math.sin(this.angle));
	}
	public void add(FreeVector b){
		// A(x,y) + B(q,w) = C(x+q,y+w)
		float x,y;
		x=this.getX()+b.getX();
		y=this.getY()+b.getY();
		this.setAngle((float)(Math.atan2(y, x)));
		this.setMagnitude((float) Math.sqrt(Math.pow(x, 2)+Math.pow(y, 2)));
	}
	public void sub(FreeVector b){
		// A(x,y) - B(q,w) = C(x-q,y-w)
		float x,y;
		x=this.getX()-b.getX();
		y=this.getY()-b.getY();
		this.setAngle((float)(Math.atan2(y, x)));
		this.setMagnitude((float) Math.sqrt(Math.pow(x, 2)+Math.pow(y, 2)));
	}
	public float dot(FreeVector b){
		// A(x,y) * B(q,w) = x*q + y*w
		return (float)((this.getX()*b.getX())+(this.getY()*b.getY()));
	}
}
